// pages/ceshi/ceshi.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sessionKey:'' ,
    phone:1122121, 
    user:null,
    modalstatus:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    app.getUserInfo(function (userInfo) {
      that.setData({
        user: userInfo
      })
      // if (userInfo.realName){
      //   that.setData({
      //     fullName: userInfo.realName,
      //   })
      // } 
      // if (userInfo.telphone) {
      //   that.setData({
      //     phoneNumber: userInfo.telphone
      //   })
      // } 
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },



  wxlogin: function () {//获取用户的openID和sessionKey
    var that = this;
    wx.login({
      //获取code 使用wx.login得到的登陆凭证，用于换取openid
      success: (res) => {
        wx.request({
          method: "post",
          url: 'http://192.168.210.68:8093/wechat/sysMessage/wxlogin?uid=818ac2965cc311e98db3fa163e4d6bc3',
          data: {
            code: res.code
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: (res1) => {
            console.log(res1);
            console.log('重新login：',res1)
          }
        });
      }
    });
  },
  // getPhoneNumber: function (e) {//点击获取手机号码按钮
  //   var that = this;
  //   //检测session是否过期
  //   wx.checkSession({
  //     success: function () {
  //       console.log(JSON.stringify(e))
  //       console.log(e.detail.errMsg)
  //       console.log(e.detail.iv)
  //       console.log(e.detail.encryptedData)
  //       var ency = e.detail.encryptedData;
  //       var iv = e.detail.iv;
  //       var sessionk = that.data.sessionKey;
  //       if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
  //         that.setData({
  //           modalstatus: true
  //         });
  //       } else {//同意授权
  //         wx.request({
  //           method: "post",
  //           url: 'http://192.168.210.68:8093/wechat/sysMessage/deciphering?uid=818ac2965cc311e98db3fa163e4d6bc3',
  //           data: {
  //             encrypdata: ency,
  //             ivdata: iv,
  //             sessionkey: ''
  //           },
  //           header: {
  //             'content-type': 'application/json' // 默认值
  //           },
  //           success: (res) => {
  //             wx.showToast({
  //               title: '成功',
  //               duration: 2000
  //             })
  //             console.log("解密成功~~~~~~~将解密的号码保存到本地~~~~~~~~");
  //             console.log(res);
  //             var phone = res.data;
  //             console.log(phone);
  //             that.setData({ phone: phone })
  //           }, fail: function (res) {
  //             wx.showToast({
  //               title: '失败',
  //               duration: 2000
  //             })
  //             console.log("解密失败~~~~~~~~~~~~~");
  //             console.log(res);
  //           }
  //         });
  //       }
  //     },
  //     fail: function () {
  //       console.log("session_key 已经失效，需要重新执行登录流程");
  //       that.wxlogin(); //重新登录
  //     }

  //   });
  // },
  ceshi:function(){
    console.log(11111111111111111111111111)
    wx.login({
      //获取code 使用wx.login得到的登陆凭证，用于换取openid
      success: (res) => {
        console.log(res)
        wx.request({
          method: "post",
          url: 'http://192.168.210.68:8093/wechat/sysMessage/wxlogin?uid=818ac2965cc311e98db3fa163e4d6bc3',
          data: {
            code: res.code
          },
          success: (res1) => {
            console.log(JSON.stringify(res1))
          }
        })
      }
    });
  },

  getPhoneNumber: function (e) {//点击获取手机号码按钮
    var that = this;
    wx.login({
      success: (res) => {
        console.log(res)
        console.log(JSON.stringify(e))
        console.log(e.detail.errMsg)
        console.log(e.detail.iv)
        console.log(e.detail.encryptedData)
        var ency = e.detail.encryptedData;
        var iv = e.detail.iv;
        var sessionk = that.data.sessionKey;
        if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
          that.setData({
            modalstatus: true
          });
        } else {//同意授权
          app.req({
            method: "post",
            url: 'wechat/sysMessage/deciphering',
            data: {
              encrypdata: ency,
              ivdata: iv,
              code: res.code
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success: (res) => {
              wx.showToast({
                title: '成功',
                duration: 2000
              })
              console.log("解密成功~~~~~~~将解密的号码保存到本地~~~~~~~~");
              console.log(res);
              var phone = res.data;
              console.log(phone);
              that.setData({ phone: phone })
            }, fail: function (res) {
              wx.showToast({
                title: '失败',
                duration: 2000
              })
              console.log("解密失败~~~~~~~~~~~~~");
              console.log(res);
            }
          });
        }
      }
    })
      
        
      
      

    
  },
})





          // wx.login({
          //   //获取code 使用wx.login得到的登陆凭证，用于换取openid
          //   success: (res) => {
          //     wx.request({
          //       method: "post",
          //       url: 'http://192.168.210.68:8093/wechat/sysMessage/wxlogin?uid=80cc12b659cc11e98db3fa163e4d6bc3',
          //       data: {
          //         code: res.code
          //       },
          //       header: {
          //         'content-type': 'application/json' // 默认值
          //       },
          //       success: (res) => {
          //         wx.request({
          //           method: "post",
          //           url:'http://192.168.210.68:8093/wechat/sysMessage/deciphering?uid=80cc12b659cc11e98db3fa163e4d6bc3',
          //           data: {
          //             encrypdata: ency,
          //             ivdata: iv,
          //             sessionkey: ''
          //           },
          //           header: {
          //             'content-type': 'application/json' // 默认值
          //           },
          //           success: (res) => {
          //             wx.showToast({
          //               title: '成功',
          //               duration: 2000
          //             })
          //             console.log("解密成功~~~~~~~将解密的号码保存到本地~~~~~~~~");
          //             console.log(res);
          //             var phone = res.data;
          //             console.log(phone);
          //             that.setData({ phone: phone })
          //           }, fail: function (res) {
          //             wx.showToast({
          //               title: '失败',
          //               duration: 2000
          //             })
          //             console.log("解密失败~~~~~~~~~~~~~");
          //             console.log(res);
          //           }
          //         });
          //       }
          //     });
          //   }
          // });